import Vue from 'vue';
import Vuex from 'vuex';
import router from '@/router/index';
import VuexPersist from 'vuex-persist';

Vue.use(Vuex);

// 创建一个存储实例
const vuexLocal = new VuexPersist({
  storage: window.localStorage, // 选择存储引擎
});

const store = new Vuex.Store({
  state: {
    menus: [],
    token: '',
    defaultAccountInfo: {
      loginTime: '', // 登录时间戳 维持两小时登录状态 两小时后需要重新登录(清空token)
      userId: '',
      account: 'admin', // 账号
      password: '123', // 密码
      avatar: require('../assets/image/defaultAvatar.png'),
    },
    screenWidth: 0,
  },
  mutations: {
    setToken(state, token) {
      state.token = token;
    },
    resetToken(state) {
      state.token = '';
    },
    initMenus(state) {
      console.log('初始化menus');
      console.log(router.options.routes[1].children);
      state.menus = router.options.routes[1].children;
    },
    updateLoginTime(state) {
      state.defaultAccountInfo.loginTime = new Date().getTime();
    },
    updateAccountInfo(state, data) {
      state.defaultAccountInfo.account = data.account;
      state.defaultAccountInfo.password = data.password;
      state.defaultAccountInfo.userId = data.userId || state.defaultAccountInfo.userId;
    },
    setScreenWidth(state, data) {
      state.screenWidth = data;
    },
  },
  plugins: [vuexLocal.plugin],
});

export default store;
